﻿using UnityEngine;
using UnityEngine.Events;

/// <summary>
/// A flexible handler for void events in the form of a MonoBehaviour. Responses can be connected directly from the Unity Inspector.
/// </summary>
public abstract class EventChannelListener<T> : MonoBehaviour
{
    [SerializeField] private EventChannelSO<T> _channel = default;

    public UnityEvent<T> OnEventRaised;

    private void Awake()
    {
        if (_channel != null)
            _channel.OnEventRaised += Respond;
    }

    private void OnDestroy()
    {
        if (_channel != null)
            _channel.OnEventRaised -= Respond;
    }

    private void Respond(T value)
    {
        if (OnEventRaised != null)
            OnEventRaised.Invoke(value);
    }
}

public class IntEventChannelListener : EventChannelListener<int>
{
}